﻿using System;
using System.IO;
using System.Text.RegularExpressions;

namespace MvcFckEditor.Components
{
    internal sealed class Util
    {
        private Util()
        { }

        public static DirectoryInfo CreateDirectory(string path)
        {
            DirectoryInfo oDir = new DirectoryInfo(Path.GetFullPath(path));

            try
            {
                if (!oDir.Exists)
                    oDir.Create();

                return oDir;
            }
            catch
            {
               return new DirectoryInfo(path);
            }
        }

        public static bool ArrayContains(Array array, object value, System.Collections.IComparer comparer)
        {
            foreach (object item in array)
            {
                if (comparer.Compare(item, value) == 0)
                    return true;
            }
            return false;
        }

        public static string ArrayToSrting(string[] arr)
        {
            string strV = string.Empty;
            foreach(string str in arr)
            {
                strV = strV + "," + str;
            }
            if (strV != string.Empty)
            {
                strV = strV.Substring(1);
            }
            return strV;
        }

        #region 过滤特殊符合
        // Do a cleanup of the folder name to avoid possible problems
        public static string SanitizeFolderName(string folderName)
        {
            // Remove . \ / | : ? * " < >
            return Regex.Replace(folderName, @"[.\\/|:?*""<>\p{C}]", "_", RegexOptions.None);
        }

        // Do a cleanup of the file name to avoid possible problems
        public static string SanitizeFileName(string fileName)
        {
            // Remove \ / | : ? * " < >
            return Regex.Replace(fileName, @"[\\/|:?*""<>\p{C}]", "_", RegexOptions.None);
        }
        #endregion

    }
}
